package main

import (
	"fmt"
	"image/color"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/storage"
	"fyne.io/fyne/v2/widget"
	themex "github.com/zhaocy/fyne-theme"
)

func main() {
	myApp := app.New()
	myApp.Settings().SetTheme(themex.Themex())
	myWin := myApp.NewWindow("dialog")

	lbl := widget.NewLabel("")
	items := []*widget.FormItem{}
	items = append(items, widget.NewFormItem("姓名", widget.NewEntry()))
	// 创建一个表单对话框
	dconf := dialog.NewForm("表单对话框", "确认", "取消", items, func(b bool) {
		if !b {
			return
		}
		lbl.SetText("ok")
	}, myWin)

	btn := widget.NewButton("表单对话框", func() {
		// 改变对话框弹窗的默认大小
		dconf.Resize(fyne.NewSize(340, 200))
		dconf.Show()
	})

	//确认对话框
	confirmD := dialog.NewConfirm("标题", "msg", func(b bool) {

	}, myWin)
	btn2 := widget.NewButton("确认对话框", func() {
		confirmD.Show()
	})

	// 颜色对话框
	d3 := dialog.NewColorPicker("颜色", "msg", func(c color.Color) {
		lbl.SetText(fmt.Sprintf("%v", c))
	}, myWin)

	btn3 := widget.NewButton("颜色对话框", func() {
		// 显示高级选项
		d3.Advanced = true
		d3.Show()
	})

	//文件对话框
	d4 := dialog.NewFileOpen(func(uc fyne.URIReadCloser, err error) {
		if uc != nil {
			lbl.SetText(uc.URI().Path())
		}
	}, myWin)
	btn4 := widget.NewButton("文件对话框", func() {
		d4.Resize(fyne.NewSize(400, 400))
		//过滤
		d4.SetFilter(storage.NewExtensionFileFilter([]string{".png"}))
		d4.Show()
	})

	// 保存对话框
	d5 := dialog.NewFileSave(func(uc fyne.URIWriteCloser, err error) {
		//点击取消按钮时返回
		if uc == nil {
			return
		}
		//写文件
		uc.Write([]byte{'a', 'b'})
		lbl.SetText(uc.URI().Path())
	}, myWin)
	d5.Resize(fyne.NewSize(500, 400))
	d5.SetFileName("test.txt")

	uri := storage.NewFileURI("./")
	lu, _ := storage.ListerForURI(uri)
	d5.SetLocation(lu)

	btn5 := widget.NewButton("保存对话框", func() {
		d5.Show()
	})

	//目录对话框
	d6 := dialog.NewFolderOpen(func(lu fyne.ListableURI, err error) {
		if lu == nil {
			return
		}
		lbl.SetText(lu.Path())
	}, myWin)

	luri, _ := storage.ListerForURI(storage.NewFileURI("."))
	d6.SetLocation(luri)
	btn6 := widget.NewButton("目录对话框", func() {
		d6.Show()
	})

	// 自定义对话框
	customLabl := widget.NewLabel("自定义")
	d7 := dialog.NewCustom("自定义标题", "取消", customLabl, myWin)
	d7.Resize(fyne.NewSize(400, 300))
	btn7 := widget.NewButton("自定义对话框", func() {
		d7.Show()
	})

	// layout
	myWin.SetContent(container.NewVBox(lbl, btn, btn2, btn3, btn4, btn5, btn6, btn7))
	myWin.Resize(fyne.NewSize(550, 450))
	myWin.ShowAndRun()
}
